package com.workingpub.commons.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

/**
 * 全局日期类型转字符串定义
 * 
 * @author taoping
 *
 */
public final class DateFormatUtil {
	private static DateFormat dateFormat;
	private static DateFormat dateTimeFormat;
	private static DateFormat timeFormat;
	private static DateFormat monthFormat;
	private static DateFormat yearFormat;

	private DateFormatUtil() {

	}

	public static String formatDate(Date date) {
		return null == date ? null : getDateformat().format(date);
	}

	public static Date parseDate(String source) {
		Date result = null;
		try {
			result = StringUtils.hasText(source) ? getDateformat().parse(source) : null;
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String formateDatetime(Date datetime) {
		return null == datetime ? null : getDatetimeformat().format(datetime);
	}

	public static Date parseDatetime(String source) {
		Date result = null;
		try {
			result = StringUtils.hasText(source) ? getDatetimeformat().parse(source) : null;
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String formateTime(Date time) {
		return null == time ? null : getTimeformat().format(time);
	}

	public static Date parseTime(String source) {
		Date result = null;
		try {
			result = StringUtils.hasText(source) ? getTimeformat().parse(source) : null;
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String formateMonth(Date source) {
		return null == source ? null : getMonthFormat().format(source);
	}

	public static Date parseMonth(String source) {
		Date result = null;
		try {
			result = StringUtils.hasText(source) ? getMonthFormat().parse(source) : null;
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public static String formateYear(Date source) {
		return null == source ? null : getYearFormat().format(source);
	}

	public static Date parseYear(String source) {
		Date result = null;
		try {
			result = StringUtils.hasText(source) ? getYearFormat().parse(source) : null;
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	private static DateFormat getDateformat() {
		if (null == dateFormat) {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		}
		return dateFormat;
	}

	private static DateFormat getDatetimeformat() {
		if (null == dateTimeFormat) {
			dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		return dateTimeFormat;
	}

	private static DateFormat getTimeformat() {
		if (null == timeFormat) {
			timeFormat = new SimpleDateFormat("HH:mm:ss");
		}
		return timeFormat;
	}

	private static DateFormat getMonthFormat() {
		if (null == monthFormat) {
			monthFormat = new SimpleDateFormat("yyyy-MM");
		}
		return monthFormat;
	}

	private static DateFormat getYearFormat() {
		if (null == yearFormat) {
			yearFormat = new SimpleDateFormat("yyyy");
		}
		return yearFormat;
	}

}
